/**
 * 
 */
package com.ofek.eventos.db;

import java.util.ArrayList;
import java.util.Iterator;

import com.ofek.base.db.ComplexDataField;
import com.ofek.base.db.DataField;
import com.ofek.base.db.DataTypeClass;
import com.ofek.db.Table;
import com.ofek.eventos.net.lks.IMap;
import com.ofek.eventos.net.lks.ItemVersion;
import com.ofek.eventos.net.lks.test.Event;
import com.ofek.eventos.net.lks.test.EventID;

/**
 * @author Ofek
 *
 */
public class EventsTable implements IMap<EventID, ItemVersion<EventID,Event>> {
	private Table<Event> eventsTable;
	@DataTypeClass
	public static class EventVersion {
		@ComplexDataField
		EventID id;
		@DataField
		long version;
		@ComplexDataField
		Event item;

		public EventVersion(final ItemVersion<EventID, Event> iv) {
			id = iv.id;
			item = iv.item;
			version = iv.version;
		}



	}

	private final Table<EventVersion> table;
	/**
	 * @param name 
	 * 
	 */
	public EventsTable(final String name) {
		table = new Table<EventVersion>(name,EventVersion.class);
	}


	@Override
	public Iterable<ItemVersion<EventID, Event>> values() {
		final ArrayList<EventVersion> all = table.getAll();
		return new Iterable<ItemVersion<EventID,Event>>() {

			@Override
			public Iterator<ItemVersion<EventID, Event>> iterator() {
				return new Iterator<ItemVersion<EventID,Event>>() {
					Iterator<EventVersion> it = all.iterator();
					@Override
					public void remove() {
						throw new UnsupportedOperationException();
					}

					@Override
					public ItemVersion<EventID, Event> next() {
						final EventVersion next = it.next();
						return new ItemVersion<>(next.id,next.item,next.version);
					}

					@Override
					public boolean hasNext() {
						return it.hasNext();
					}
				};
			}
		};
	}
	@Override
	public ItemVersion<EventID, Event> remove(final EventID key) {
		final EventVersion eventVersion = table.get("id_id="+key.id+" and id_owner="+key.owner);
		if ( eventVersion!=null ) {
			table.remove("id_id="+key.id+" and id_owner="+key.owner);
			return new ItemVersion<>(eventVersion.id,eventVersion.item,eventVersion.version);
		}
		return null;
	}
	/* (non-Javadoc)
	 * @see com.ofek.eventos.db.EventosDatabase.VersionTable#put(java.lang.Object, com.ofek.eventos.net.lks.ItemVersion)
	 */
	@Override
	public ItemVersion<EventID, Event> put(final EventID key,
			final ItemVersion<EventID, Event> newValue) {
		final ArrayList<EventVersion> query = table.query("SELECT * FROM "+table.getTableName() + " where id_id="+key.id+" and id_owner="+key.owner+" ORDER BY version DESC LIMIT 1;");
		table.add(new EventVersion(newValue));
		if (query.size()<=0)return null;
		final EventVersion eventVersion = query.get(0);
		return  new ItemVersion<>(eventVersion.id,eventVersion.item,eventVersion.version);
	}
	/* (non-Javadoc)
	 * @see com.ofek.eventos.db.EventosDatabase.VersionTable#get(java.lang.Object)
	 */
	@Override
	public ItemVersion<EventID, Event> get(final EventID key) {
		final EventVersion eventVersion = table.get("id_id="+key.id+" and id_owner="+key.owner);
		if (eventVersion==null) return null;
		return  new ItemVersion<>(eventVersion.id,eventVersion.item,eventVersion.version);
	}
}

